package com.lkj;

public class HexUtil {

    /**
     * 字符串转byte[]
     * @param str
     * @return
     */
    public static byte[] strToByteArray(String str){

        if (str == null){
            return null;
        }

        byte[] bytes = str.getBytes();

        return bytes;
    }

    /**
     * byte[]转字符串
     * @param bytes
     * @return
     */
    public static String byteArrayToStr(byte[] bytes){
        if (bytes == null){
            return null;
        }
        String str = new String(bytes);

        return str;
    }

    /**
     * byte[]转十六进制字符串
     * @param bytes
     * @return
     */
    public static String byteArrayToHexStr(byte[] bytes){
        if (bytes == null){
            return null;
        }
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    /**
     * 将16进制字符串转换为byte[]
     *
     * @param str
     * @return
     */
    public static byte[] hexStrToByteArray(String str) {
        if(str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for(int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        return bytes;
    }

    /**
     * 字符串转十六进制字符串
     * @param string
     * @return
     */
    public static String strToHexStr(String string){

        if (string == null){
            return null;
        }

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            builder.append(Integer.toHexString(c));
        }

        return builder.toString();
    }

    /**
     * 字符串转十六进制字符串
     * @param str
     * @return
     */
    public static String str2HexStr(String str) {
        if (str == null){
            return null;
        }
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
        }
        return sb.toString().trim();
    }


    /**
     * 十六进制字符转文本字符串
     * @param hexStr
     * @return
     */
    public static String       hexStrToStr(String hexStr) {
        if (hexStr == null){
            return null;
        }
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }

        return new String(bytes);
    }
}
